package com.sky.course.controller;

import com.sky.course.entity.FileDTO;
import com.sky.course.service.QCloudFile;
import com.sky.course.util.FileUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
* @Description:    文件管理工具组
* @Author:         Joe
* @CreateDate:     2019/10/29 19:51
*/
@RestController
@RequestMapping(value = "/file")
@Api(value = "文件管理",description = "文件管理")
public class FileController {

    @Autowired
    private QCloudFile qCloudFile;

    //private String defaultKey;

    @ApiOperation(value = "文件上传",notes = "文件上传")
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public FileDTO upload(@ApiParam(value = "上传的文件",required = true) MultipartFile file){
        String defaultKey = "course/";
        File newFile = FileUtils.multipartFileToFile(file);
        try {
            String fileName = qCloudFile.uploadfile(newFile,defaultKey);
            FileDTO fileDTO = new FileDTO();
            fileDTO.setSuccess(1);
            fileDTO.setUrl(fileName);
            return fileDTO;
        }catch (Exception e){
            e.printStackTrace();
        }
        FileDTO fileDTO = new FileDTO();
        fileDTO.setSuccess(1);
        fileDTO.setUrl("/images/a.jpg");
        return fileDTO;
    }
}
